package com.aliwo.iterator;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @author xuyayuan
 * @data 2021年06月22日
 */
public class IteratorTest {
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(new Person("xuyy", "男", "1233"));
        list.add(new Person("zwm", "女", "1234"));
        list.add(new Person("xtt", "男", "1235"));
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next.toString());
        }
        // 如果需要再次遍历，需要重置我们的迭代器
        System.out.println("第二次遍历:");
        iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next.toString());
        }
        // 使用增强for循环，底层仍然是迭代器
        System.out.println("使用增强for循环进行遍历");
        for (Object o : list) {
            System.out.println(o);
        }

    }
}

class Person {
    private String name;
    private String sex;
    private String telphone;

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", telphone='" + telphone + '\'' +
                '}';
    }

    public Person(String name, String sex, String telphone) {
        this.name = name;
        this.sex = sex;
        this.telphone = telphone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTelphone() {
        return telphone;
    }

    public void setTelphone(String telphone) {
        this.telphone = telphone;
    }
}